﻿/***************************  Ccay IGrid Drag  ***********************************/
(function(IGrid) {
	var drag = IGrid._Drag = {
		/** @description grid的拖拽
		 *  @param {Object} grid IGrid对象
		 *  @param {Object} op grid的配置信息
		 */
		gridDrag: function (grid, op) {
			op.dragOp || (op.dragOp = {});
			var container = op.dragOp.container || grid.dom,
				bodyPanel = grid.getDataBody(),
				fnDrag = op.dragOp.fnDrag,
				fnMove = op.dragOp.fnMove,
				fnBeginDrop = op.dragOp.fnBeginDrop,
				fnEndDrop = op.dragOp.fnEndDrop;
			container.addClass("ccay-igrid-drag").drop($.extend(op.dragOp, {
				clickable: true,
				panels: bodyPanel,
				onDragStart: function(e, pos, mover, trigger, op) {
					if(fnDrag && fnDrag.call(grid, e, pos, mover, trigger, op) === false) {
						return false;
					}
				},
				onMoving: function (e, pos, mover, trigger, op) {
					if(fnMove && fnMove.call(grid, e, pos, mover, trigger, op) === false) {
						return false;
					}
				},
				onDroped: function(e, pos, mover, trigger, op){
					if(!fnBeginDrop || fnBeginDrop.call(grid, e, pos, mover, trigger, op) !== false) {
						fnEndDrop && fnEndDrop.call(grid, e, pos, mover, trigger, op);
					}
				}
			}));
		},
		/** @description 普通的行转动拖拽
		 *  @param {Object} e 事件对象
		 *  @param {Object} pos 定位参数
		 *  @param {Object} mover 拖拽面板对象
		 *  @param {Object} trigger 拖拽对象
		 *  @param {Object} dropOp 拖拽配置
		 *  @param {Object} grid IGrid对象
		 */
		commonRowTurn: function (e, pos, mover, trigger, dropOp, grid) {
			var storage = grid.op.Storage;
			if (dropOp.actives) {
				// 获取目标对象
				var target = dropOp.actives.item, 
					dataset = storage.dataset, 
					// 获取当前拖拽对象数据
					currentData = grid.getRowData(trigger),
					// 获取当前拖拽对象数据索引
					currentIdx = currentData._index,
					targetData, targetIdx, tempInsertData;
				if (target) {
					// 将当前拖动对象插入目标对象前
					target.before(trigger);
					// 获取目标对象数据
					targetData = grid.getRowData(target);
					// 获取目标对象数据索引
					targetIdx = targetData._index;
					// 移除当前拖拽对象数据
					dataset.splice(currentIdx, 1);
					// 若当前数据在目标数据前，则需目标对象索引 - 1，否则索引不变
					if (currentIdx < targetIdx) {
						targetIdx = targetIdx - 1;
					}
					// 在数据集中将目标对象数据前添加该拖拽数据
					dataset.splice(targetIdx, 0, currentData);
				} else {
					// 将当前拖动对象插入最后
					dropOp.actives.panel.append(trigger);
					// 移除当前拖拽对象数据
					dataset.splice(currentIdx, 1);
					// 在数据集中将当前对象加进去
					dataset.push(currentData);
				}
				// 重新加载grid
				grid.reload();
			}
		}
	};
	
	IGrid.Drag = $.IAdapter();
	
	Ccay.UI.XControl.addPlugin("XGrid", "drag", {
	    build: function (grid, op) {
	    	if(op.dragObj){
	    		var dragObj = IGrid.Drag.get(op.dragObj);
	    		dragObj && dragObj.setOption(grid, op);
	    		op.handler.complete.add(drag.gridDrag);
	    	}
	    }
	});
	
	// 注册行拖拽插件
	IGrid.Drag.add("row",{
		setOption: function (grid, op) {
			op.dragOp || (op.dragOp = {});
			var fnDrag = op.dragOp.fnDrag, fnBeginDrop = op.dragOp.fnBeginDrop, 
				_dragOp = {
					target: ".grid-row", 
					signMode: "ver",
					offset: true,
					fnDrag: function (e, pos, mover, trigger, op) {
						if(!fnDrag || fnDrag.call(grid, e, pos, mover, trigger, op) !== false) {
							mover.width(grid.dom.width()).text(trigger.children("td:eq(2)").text());
						} else {
							return false;
						}
					},
					fnBeginDrop: function (e, pos, mover, trigger, op) {
						if(!fnBeginDrop || fnBeginDrop.call(grid, e, pos, mover, trigger, op) !== false) {
							drag.commonRowTurn(e, pos, mover, trigger, op, grid);
						} else {
							return false;
						}
					}
				};
			$.extend(op.dragOp, _dragOp);
		}
	});
	
	// 注册单元格拖拽插件
	/*IGrid.Drag.add("cell",{
		setOption: function (grid, op) {}
	});*/
})(Ccay.UI.IGrid);